package com.gaojice.ftp.server;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import jftp.FtpConnection;
import jftp.FtpServer;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.gaojice.server.http.HttpService;

public class FtpService extends Service {

	private HttpService httpService;
	public static String uitemplate;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		try {
			uitemplate = setUiTemplate();
		} catch (IOException e) {
			e.printStackTrace();
		}
		FtpConnection.root = "/sdcard/ftp"; // 共享目录
		Log.i(FtpServer.class.getName(), "ftp server root: "
				+ FtpConnection.root);
		httpService = new HttpService(); // 启动线程
		httpService.start();
		Toast.makeText(this, "服务已启动...", Toast.LENGTH_LONG).show();
	}

	private String setUiTemplate() throws IOException {
		InputStream inputStream = getApplicationContext().getResources()
				.openRawResource(R.raw.uitemplate);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				inputStream);
		byte data[] = new byte[1024];
		int num = 0;
		StringBuffer stringBuffer = new StringBuffer();
		while ((num = bufferedInputStream.read(data)) > 0) {
			stringBuffer.append(new String(data, 0, num));
		}
		bufferedInputStream.close();
		return stringBuffer.toString();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		httpService.stopService();
		Toast.makeText(this, "服务已停止...", Toast.LENGTH_LONG).show();
	}

}
